﻿using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace AutoTranslate.Helps {
    public static class FontHelper {
        public static System.Drawing.Font GetFont([In][Out] TextBlock textBlock) {
            return new System.Drawing.Font(
                familyName: textBlock.FontFamily.Source,
                emSize: (float)textBlock.FontSize,
                style: (textBlock.FontWeight == FontWeights.Bold ? System.Drawing.FontStyle.Bold : System.Drawing.FontStyle.Regular)
                        | (textBlock.FontStyle == FontStyles.Oblique ? System.Drawing.FontStyle.Italic : System.Drawing.FontStyle.Regular)
                        | (textBlock.TextDecorations.Contains(TextDecorations.Underline[0]) ? System.Drawing.FontStyle.Underline : System.Drawing.FontStyle.Regular)
                        | (textBlock.TextDecorations.Contains(TextDecorations.Strikethrough[0]) ? System.Drawing.FontStyle.Strikeout : System.Drawing.FontStyle.Regular));
        }

        public static System.Drawing.Color GetColor([In][Out] TextBlock textBlock) {
            var color = ((SolidColorBrush)textBlock.Foreground).Color;
            return System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
        }

        public static void SetFont([In][Out] TextBlock textBlock, [In] System.Drawing.Font font) {
            textBlock.FontFamily = new FontFamily(font.FontFamily.Name);
            textBlock.FontSize = font.Size;
            textBlock.FontWeight = font.Bold ? FontWeights.Bold : FontWeights.Normal;
            textBlock.FontStyle = font.Italic ? FontStyles.Oblique : FontStyles.Normal;
            var textDecorations = new TextDecorationCollection();
            if (font.Underline)
                textDecorations.Add(TextDecorations.Underline);
            if (font.Strikeout)
                textDecorations.Add(TextDecorations.Strikethrough);
            textBlock.TextDecorations = textDecorations;
        }

        public static void SetColor([In][Out] TextBlock textBlock, [In] System.Drawing.Color color) {
            textBlock.Foreground = new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
        }
    }
}
